<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
	pageEncoding="ISO-8859-1"%>
<%@ page import="java.sql.*"%>
<%@ page import="java.io.*"%>
<%@ page import="java.util.*"%>
<%@page import="Logic.Authorizer,Database.DBConnection"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<%
String KeywVar=request.getParameter("Keyw");
String KeywVar2=request.getParameter("Keyw2");
String Searchtype=request.getParameter("SearchT").toString().trim();
String SearchtypeLastU=Searchtype+" Last Update";
System.out.println(KeywVar);
System.out.println(Searchtype);
System.out.println(SearchtypeLastU);

%>
<jsp:include page="t_header.jsp" />
<div class="breadcrumbs_container">
	<article class="breadcrumbs"> <a href="Proc_Process.jsp">Process</a>
	<div class="breadcrumb_divider"></div>
	<a class="current">Search Results</a> </article>
</div>
<jsp:include page="header.html" />

<!-- Declare sidebar -->
<jsp:include page="t_sidebarProcess.jsp" />

<!-- Main Content -->
<head>
<style type="text/css" >    
            .pg-normal {
            	font-size:13px;
                color: black;
                font-weight: normal;
                text-decoration: none;    
                cursor: pointer;    
            }
            .pg-selected {
            	font-size:13px;
                color: blue;
                font-weight: bold;        
                text-decoration: underline;
                cursor: pointer;
            }
        </style>
<script type="text/javascript" src="paging.js"></script>
</head>
<body>
<section id="main" class="column"> 
<article class="module width_full">
		<header><h3>Search Results (<%=Searchtype.toUpperCase()+"S"%>)</h3></header>
		<table id='results' class="tablesorter" cellspacing="0">
		<thead>
				<tr>
					<th>Process Name</th>
					<th style="width:80px;">Code</th>
					<th><%=Searchtype%></th>
					<th style="width:150px;"><%=SearchtypeLastU %></th>
				</tr>
		</thead>
		<tbody>
		<%
		if(Searchtype.equals("Procedure"))
		{
		boolean SeeResults=false;
			try {
				 String connectionURL1st = "jdbc:mysql://localhost:3306/qmsdb";
				 Connection connection1st = null;
				 Statement statement1st = null;
				 ResultSet rs1st = null;

				 connection1st = DBConnection.connect(true);
				 statement1st = connection1st.createStatement();
				 String QueryString1st = "SELECT ProcessName,Code,ProcedureName,ProcedureURL,ProcLastUpdate FROM qmsdb.processes WHERE ProcessName LIKE '%"+KeywVar2+"%' AND ProcedureName LIKE '%"+KeywVar+"%' Order by Code";
				 rs1st = statement1st.executeQuery(QueryString1st);
			
				 while (rs1st.next())
				 {				 	
					 %>
					 <tr>
					 <td><Label style="padding-left:5px;"><%=rs1st.getString("ProcessName")%></Label></td>
					 <td><Label style="padding-left:5px;"><%=rs1st.getString("Code")%></Label></td>
					 <td><Label style="padding-left:5px;"><a href="<%=rs1st.getString("ProcedureURL")%>"><%=rs1st.getString("ProcedureName")%></a></Label></td>
					 <td><Label style="padding-left:5px;"><%=rs1st.getString("ProcLastUpdate")%></Label></td>
					 </tr>
					<%
					SeeResults=true;
				 }		
					if(SeeResults==false)
					{
						System.out.println("false");
					%>
				 	<tr>
				 	<th colspan="4" style="padding-top:30px;"><Label style="font-size:18px; color:#8A0808;">No Records Found!</Label></th>
				 	</tr>
				 	<%
					}
					
				// close all the connections.
				rs1st.close();
				statement1st.close();
				connection1st.close();	
				}
				catch (Exception ex) {
				}
				finally{
				}
				}
		else if(Searchtype.equals("Form"))
		{
		boolean SeeResults=false;
			try {
				 Connection connection1st = null;
				 Statement statement1st = null;
				 ResultSet rs1st = null;

				 connection1st = DBConnection.connect(true);
				 statement1st = connection1st.createStatement();
				 String QueryString1st = "SELECT KP.ProcessName,KP.Code,KP.ProcedureName,F.FormName,F.FormURL,F.FormLastUpdate FROM qmsdb.processes AS KP INNER JOIN qmsdb.forms AS f ON KP.idProc=F.idProc WHERE KP.ProcessName Like '%"+KeywVar2+"%' AND F.FormName Like '%"+KeywVar+"%' Order by KP.Code";
				 rs1st = statement1st.executeQuery(QueryString1st);
		
				 while (rs1st.next())
				 {
						String ProcedureNameVar=rs1st.getString("KP.ProcedureName");
				 		while(ProcedureNameVar.indexOf(" ")!=-1)
						{
							int indexVar=ProcedureNameVar.indexOf(" ");
							ProcedureNameVar = ProcedureNameVar.substring(0,indexVar)+ProcedureNameVar.substring(indexVar+1);
						}				 
				 
					 %>
					 <tr>
					 <td><Label style="padding-left:5px;"><%=rs1st.getString("KP.ProcessName")%></Label></td>
					 <td><Label style="padding-left:5px;"><%=rs1st.getString("KP.Code")%></Label></td>
					 <td><Label style="padding-left:5px;"><a href="<%=rs1st.getString("F.FormURL")%>" title=<%=ProcedureNameVar%>><%=rs1st.getString("F.FormName")%></a></Label></td>
					 <td><Label style="padding-left:5px;"><%=rs1st.getString("F.FormLastUpdate")%></Label></td>
					 </tr>
					<%
				 	SeeResults=true;
				 }	 	
				 if(SeeResults==false)
				 {
				 	System.out.println("false");
				 	%>
				 	<tr>
				 	<th colspan="4" style="padding-top:30px;"><Label style="font-size:18px; color:#8A0808;">No Records Found!</Label></th>
				 	</tr>
				 	<%
				 }
				// close all the connections.
				rs1st.close();
				statement1st.close();
				connection1st.close();	
				}
				catch (Exception ex) {
				}
				finally{
				}
		}
		else
		{
			//No keyword passed
		}
		%>
		</tbody>
			</table>
			<div class="submit_link">
			</div>
			<div id="pageNavPosition" style="width:600px; margin-top:15px; margin-left:315px;">
		   	</div>
		   	<div class="spacer"></div>
			<footer>
			<div class="submit_link">
	  		<input id="Back" name="Back" type="submit" value="Back" class="alt_btn" onclick="location.href='Proc_SearchProcess.jsp'">
	  		</div>
	  		</footer>
	</article>
	<script type="text/javascript">
<!--
    var pager = new Pager('results', 15); 
    pager.init(); 
    pager.showPageNav('pager', 'pageNavPosition'); 
    pager.showPage(1);
    
//-->
function Pager(tableName, itemsPerPage) {
    this.tableName = tableName;
    this.itemsPerPage = itemsPerPage;
    this.currentPage = 1;
    this.pages = 0;
    this.inited = false;
    
    this.showRecords = function(from, to) {        
        var rows = document.getElementById(tableName).rows;
        for (var i = 1; i < rows.length; i++) {
            if (i < from || i > to)  
                rows[i].style.display = 'none';
            else
                rows[i].style.display = '';
        }
    }
    this.showPage = function(pageNumber) {
    	if (! this.inited) {
    		alert("not inited");
    		return;
    	}
        var oldPageAnchor = document.getElementById('pg'+this.currentPage);
        oldPageAnchor.className = 'pg-normal';
        
        this.currentPage = pageNumber;
        var newPageAnchor = document.getElementById('pg'+this.currentPage);
        newPageAnchor.className = 'pg-selected';
        
        var from = (pageNumber - 1) * itemsPerPage + 1;
        var to = from + itemsPerPage - 1;
        this.showRecords(from, to);
    }     
    this.prev = function() {
        if (this.currentPage > 1)
            this.showPage(this.currentPage - 1);
    }    
    this.next = function() {
        if (this.currentPage < this.pages) {
            this.showPage(this.currentPage + 1);
        }
    }                           
    this.init = function() {
        var rows = document.getElementById(tableName).rows;
        var records = (rows.length - 1); 
        this.pages = Math.ceil(records / itemsPerPage);
        this.inited = true;
    }
    this.showPageNav = function(pagerName, positionId) {
    	if (! this.inited) {
    		alert("not inited");
    		return;
    	}
    	var element = document.getElementById(positionId);
    	
    	var pagerHtml = '<span onclick="' + pagerName + '.prev();" class="pg-normal"> &#171 Prev </span> | ';
        for (var page = 1; page <= this.pages; page++) 
            pagerHtml += '<span id="pg' + page + '" class="pg-normal" onclick="' + pagerName + '.showPage(' + page + ');">' + page + '</span> | ';
        pagerHtml += '<span onclick="'+pagerName+'.next();" class="pg-normal"> Next &#187;</span>';            
        
        element.innerHTML = pagerHtml;
    }
}
</script>
</section>
<div class="spacer"></div>
<!-- Declare closing tags -->
<jsp:include page="t_footer.jsp" />
</body>